package com.zq.behavior.mediator;

import java.util.Objects;
import java.util.UUID;

public interface User {
    String getUserId();
    ChatRoom<? extends User> getChatRoom();
    void send(String to, String content);
    void receive(String fromId, String content);
}

class Person implements User {
    ChatRoom<Person> chatRoom;
    String id;

    Person(ChatRoom<Person> chatRoom) {
        this.id = UUID.randomUUID().toString();
        this.chatRoom = Objects.requireNonNull(chatRoom);
        this.chatRoom.register(this);
    }

    @Override
    public String getUserId() {
        return id;
    }

    @Override
    public ConcreteChatRoom getChatRoom() {
        return (ConcreteChatRoom) chatRoom;
    }

    @Override
    public void send(String to, String content) {
        getChatRoom().send(id, to, content);
    }

    @Override
    public void receive(String fromId, String content) {
        System.out.println("user:" + fromId + "has sent a msg(" + content + ") to me(id=" + id + ")");
    }
}
